import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';

/// 自定义对话框组件，可在全应用范围内复用
class CustomDialog extends StatelessWidget {
  final String title;
  final String content;
  final String? cancelText;
  final String confirmText;
  final VoidCallback? onCancel;
  final VoidCallback onConfirm;
  final Color? confirmColor;

  const CustomDialog({
    super.key,
    required this.title,
    required this.content,
    this.cancelText,
    required this.confirmText,
    this.onCancel,
    required this.onConfirm,
    this.confirmColor,
  });

  /// 显示对话框的静态方法
  static Future<bool?> show({
    required BuildContext context,
    required String title,
    required String content,
    String? cancelText,
    required String confirmText,
    VoidCallback? onCancel,
    required VoidCallback onConfirm,
    Color? confirmColor,
  }) {
    return showDialog<bool>(
      context: context,
      builder:
          (context) => CustomDialog(
            title: title,
            content: content,
            cancelText: cancelText,
            confirmText: confirmText,
            onCancel: onCancel ?? () => Navigator.of(context).pop(false),
            onConfirm: () {
              // 安全地关闭对话框并执行回调
              if (Navigator.canPop(context)) {
                Navigator.of(context).pop(true);
              }
              onConfirm();
            },
            confirmColor: confirmColor,
          ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      elevation: 0,
      child: Container(
        width: 325.w,
        padding: EdgeInsets.all(24.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20.r),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.1),
              blurRadius: 10,
              offset: const Offset(0, 4),
            ),
          ],
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 20.sp,
                fontWeight: FontWeight.w600,
                color: const Color(0xFF2D1457),
              ),
            ),
            SizedBox(height: 16.h),
            Text(
              content,
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 16.sp, color: Colors.black87),
            ),
            SizedBox(height: 32.h),
            Row(
              children: [
                if (cancelText != null) ...[
                  Expanded(
                    child: _buildButton(
                      text: cancelText!,
                      onPressed:
                          onCancel ??
                          () {
                            if (Navigator.canPop(context)) {
                              Navigator.of(context).pop(false);
                            }
                          },
                      color: Colors.grey.shade200,
                      textColor: Colors.black87,
                    ),
                  ),
                  SizedBox(width: 16.w),
                ],
                Expanded(
                  child: _buildButton(
                    text: confirmText,
                    onPressed: () {
                      // 安全地关闭对话框并执行回调
                      if (Navigator.canPop(context)) {
                        Navigator.of(context).pop(true);
                      }
                      onConfirm();
                    },
                    color: confirmColor ?? const Color(0xFFA794FD),
                    textColor: Colors.white,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildButton({
    required String text,
    required VoidCallback onPressed,
    required Color color,
    required Color textColor,
  }) {
    return ElevatedButton(
      onPressed: onPressed,
      style: ElevatedButton.styleFrom(
        backgroundColor: color,
        foregroundColor: textColor,
        padding: EdgeInsets.symmetric(vertical: 14.h),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(30.r),
        ),
        elevation: 0,
      ),
      child: Text(
        text,
        style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
      ),
    );
  }
}
